package com.yitiao.multithread.threadnotify;

import java.util.ArrayList;
import java.util.List;

/**
 * @author libiao
 */
public class ThreadNotifyVolatile {
    //定义共享变量来实现通信，它需要volatile修饰，否则线程不能及时感知
    static volatile boolean notice = false;

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();

        //线程A
        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                list.add("abc");
                System.out.println("线程A添加元素，此时list的size为：" + list.size());
                if (list.size() == 5) {
                    notice = true;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(() -> {
            while (!notice) {

            }
            System.out.println("5次，到我了");
        }).start();
    }

}
